package combine.backend.view.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;


import com.opensymphony.xwork2.ActionSupport;

import combine.order.impl.BOMManagerImpl;
import combine.order.impl.OrderManagerImpl;
import combine.order.model.BOMManger;
import combine.order.model.OrderManger;
import combine.utils.RequestUtils;

public class BaseAction extends ActionSupport {
	
	public static final String MESSAGE_OK = "ok";
	
	public static final String MESSAGE_WRONG = "wrong";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected transient final Log log = LogFactory.getLog(getClass());

	protected static final int EXT_START = 0;// ext分页时start参数的默认值
	protected static final int EXT_LIMIT = 10;// ext分页时limit参数的默认值

	@Autowired
	protected OrderManger orderManger = new OrderManagerImpl();
	
	protected BOMManger bomManager = new BOMManagerImpl();
	
	protected String msg;
	
	protected boolean success;
	
	protected String errorMessage;
	
	/**
	 * 
	 * 分页使用的三个参数： currentPage为当前要显示的页，默认为第一页
	 * totalPage需要设置为DataPage.getTotalPageCount()方法返回的值
	 * pageSize为每页需要显示的记录数，默认为10
	 * 
	 */

	protected String totalPage;
	protected int totalCount;

	/**
	 * 把日期形式(yyyy-MM-dd HH:mm:ss)表示的字符串转换成对应的毫秒数 如果不符合不能正常转换，则返回当前时间对应的毫秒数
	 * 
	 * @param dateString
	 *            (yyyy-MM-dd HH:mm:ss)格式字符串
	 * @return
	 */
	protected Long convertDateStrToLong(String dateString) {
		if (dateString == null || dateString.trim().equals(""))
			return null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return dateFormat.parse(dateString).getTime();
		} catch (ParseException parseException) {
			return new Date().getTime();
		}
	}

	/**
	 * 把日期形式(yyyy-MM-dd )表示的字符串转换成对应的毫秒数 如果不符合不能正常转换，则返回当前时间对应的毫秒数
	 * 
	 * @param dateString
	 *            (yyyy-MM-dd )格式字符串
	 * @return
	 */
	protected Long convertDateStringToLong(String dateString) {
		if (dateString == null || dateString.trim().equals(""))
			return null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateFormat.parse(dateString).getTime();
		} catch (ParseException parseException) {
			return new Date().getTime();
		}
	}

	/**
	 * 获取Request对象
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获取Response对象
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获得一个Session对象，如果没有则创建一个新的Session
	 * 
	 * @return the session from the request (request.getSession()).
	 */
	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获得ServletContext的配置信息
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Map getConfiguration() {
		Map config = (HashMap) getSession().getServletContext()
				.getAttribute("");
		// 如果是测试用例则创建一个新的
		if (config == null) {
			return new HashMap();
		}
		return config;
	}

	protected Cookie[] getCookies() {
		return getRequest().getCookies();
	}

	protected Cookie getCookie(String cookieName) {
		return RequestUtils.getCookie(getRequest(), cookieName);
	}

	protected void removeCookie(String cookieName) {
		if (!StringUtils.isEmpty(getCookie(cookieName).getValue())) {
			RequestUtils.removeCookie(getResponse(), cookieName);
		}
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPage() {
		return Integer.parseInt(totalPage);
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage + "";
	}

	public Log getLog() {
		return log;
	}

	public void setTotalPage(String totalPage) {
		this.totalPage = totalPage;
	}

	protected void setError() {
		msg = MESSAGE_WRONG;
		success = false;
	}
	
	protected void setErrorInverse() {
		success =false;
		msg = MESSAGE_OK;
	}
	
	protected void setError(String message) {
		msg = message;
		success = false;
	}
	
	protected void setOK() {
		msg = MESSAGE_OK;
		success = true;
	}
	
	protected void setOKInverse() {
		msg = MESSAGE_WRONG;
		success = true;
	}
	
}
